package dao

import (
	"encoding/json"
	"fmt"
	"git.supremind.info/product/box/vehicle/common"
)

type UserInfoAuth struct {
	BaseModel
	UserId    int
	AuthType  string
	AuthCode  string
	AuthToken string
	AreaId    int
}

func (c *UserInfoAuth) TableName() string {
	return "user_info_auth"
}

func (c *UserInfoAuth) ToJsonStr() string {
	marshal, err := json.Marshal(c)
	if err != nil {
		return ""
	}
	return string(marshal)
}

func LoginCheck(name, password string) (UserInfoAuth, error) {
	var user UserInfoAuth
	err := DB.Where("auth_code = ? and is_del = 0", name).Find(&user).Error
	if err != nil {
		return user, err
	}

	if name != user.AuthCode || password != user.AuthToken {

	}
	//userInfo := SelectUserInfoById(user.ID)
	if password != "smai123" {
		return user, fmt.Errorf("登陆信息有误")
	}

	return user, nil
}

func SelectAllUserInfoAuth() *[]UserInfoAuth {
	infoAuth := make([]UserInfoAuth, 0)
	DB.Where("is_del = ?", 0).Find(&infoAuth)
	return &infoAuth
}

func SelectUserInfoAuthByExample(auth *UserInfoAuth) *[]UserInfoAuth {
	infoAuth := make([]UserInfoAuth, 0)
	DB.Where(&auth)
	DB.Find(&infoAuth)
	return &infoAuth
}

func SaveUserInfoAuth(auth *UserInfoAuth) int {
	DB.Create(&auth)
	return auth.ID
}

func SaveUserInfoAuthBatch(auth *[]UserInfoAuth) {
	for _, infoAuth := range *auth {
		DB.Create(&infoAuth)
	}
}

func UpdateUserInfoAuth(auth *UserInfoAuth) {
	DB.Model(&auth).Save(&auth)
	common.Logger.Infof("update userinfoauth data :%s", auth.ToJsonStr())
}

func DeleteUserInfoAuthById(id int) {
	infoAuth := UserInfoAuth{}
	infoAuth.ID = id
	DB.Delete(infoAuth)
}

func DeleteUserInfoAuthByExample(auth *UserInfoAuth) {
	DB.Where(&auth).Delete(UserInfoAuth{})
}
